﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace ConjureBivouac2012.GameStates
{
    public class GameOver : IGlobalGameState
    {
        private ContentManager _contentManager = null;
        private GraphicsDevice _device;
        private GamePadState _previousGamePadState;
        private KeyboardState _previousKeyboardState;
        private bool EnterHit = false;
        private Texture2D _background;
        private SpriteFont _font;

        public GameOver(ContentManager contentManager, GraphicsDevice device)
        {
            _contentManager = contentManager;
            _device = device;
            _previousGamePadState = GamePad.GetState(PlayerIndex.One);
            _previousKeyboardState = Keyboard.GetState();

            _font = contentManager.Load<SpriteFont>("myFont");
            _background = contentManager.Load<Texture2D>("Defeat");
        }

        public void Enter()
        {
            EnterHit = false;
        }

        public void Execute(GameTime gameTime)
        {
            if (EnterHit)
            {
                Program.GetGameInstance.SetState(new MainMenu(_contentManager, _device));
            }
            else
            {
                GamePadState gamePad = GamePad.GetState(PlayerIndex.One);
                KeyboardState keyboard = Keyboard.GetState();

                if ((keyboard.IsKeyDown(Keys.Enter) && !_previousKeyboardState.IsKeyDown(Keys.Enter)) || (gamePad.Buttons.Start == ButtonState.Pressed && _previousGamePadState.Buttons.Start == ButtonState.Released) ||
                    (gamePad.Buttons.A == ButtonState.Pressed && _previousGamePadState.Buttons.A == ButtonState.Released))
                {
                    EnterHit = true;
                }
            }

            _previousGamePadState = GamePad.GetState(PlayerIndex.One);
            _previousKeyboardState = Keyboard.GetState();
        }

        public void Exit()
        {

        }

        public void Draw(SpriteBatch theSpriteBatch)
        {
            int x, y, width, height;

            if (_background.Width >= _device.Viewport.Width)
            {
                x = 0;
                width = _device.Viewport.Width;
            }
            else
            {
                x = _device.Viewport.Width/2 - _background.Width/2;
                width = _background.Width;
            }

            if(_background.Height >= _device.Viewport.Height)
            {
                y = 0;
                height = _device.Viewport.Height;
            }
            else
            {
                y = _device.Viewport.Height / 2 - _background.Height / 2;
                height = _background.Height;
            }

            theSpriteBatch.Draw(_background, new Rectangle(x, y, width, height), null, Color.White, 0, new Vector2(0, 0),
                                        SpriteEffects.None, 1);

            string nb = String.Format("{0:0.0}", (double)GameInfo.GetInstance().SurvivedTime);         

            theSpriteBatch.DrawString(_font, "You survived " + nb + " seconds", new Vector2(x + (float)_background.Width/4 - 10, _device.Viewport.Height/2 - 10), Color.White, 0, Vector2.Zero, 1.25f, SpriteEffects.None, 0);
        }
    }
}
